package controler;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.Academy;
import model.Subject;
import model.beans.PlanEstudioBean;
import model.beans.UnidadBean;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
import org.apache.struts.actions.MappingDispatchAction;

import business.misc.Util;

public class UnitActions extends MappingDispatchAction {

	public ActionForward list(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		ActionForward action_retVal = Validate.validateUser(mapping,request, getResources(request));
    	if(action_retVal!=null)return action_retVal;
    	
    	HttpSession session = request.getSession();
    	PlanEstudioBean bn_planEstudio = (PlanEstudioBean)session.getAttribute("inst_contenido");
    	
    	String str_academyCode = request.getParameter("academy_id");
    	String str_subjectCode = request.getParameter("subject_id");
    	request.setAttribute("academia", Academy.instance(str_academyCode));    	
    	request.setAttribute("materia", Subject.instance(str_subjectCode, str_academyCode));
    	
    	request.setAttribute("lst_unidad", business.Unidades.list(bn_planEstudio.getInt_planEstudio(), str_academyCode, str_subjectCode));
    	
		return mapping.findForward(Util.DEFAULT); 
	}
	
	public ActionForward add(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		ActionForward action_retVal = Validate.validateUser(mapping,request, getResources(request));
		if(action_retVal!=null)return action_retVal;
		
		DynaActionForm forma = (DynaActionForm)form;
		forma.initialize(mapping);
		forma.reset(mapping, request);
		
		String str_academyCode = request.getParameter("academy_id");
		String str_subjectCode = request.getParameter("subject_id");
		request.setAttribute("academia", Academy.instance(str_academyCode));
		request.setAttribute("materia", Subject.instance(str_subjectCode, str_academyCode));
		return mapping.findForward(Util.DEFAULT); 
	}
	
	public ActionForward edit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		ActionForward action_retVal = Validate.validateUser(mapping,request, getResources(request));
		if(action_retVal!=null)return action_retVal;
		
		int int_unitKey = Integer.parseInt(request.getParameter("unit_id"));
		String str_academyCode = request.getParameter("academy_id");
		String str_subjectCode = request.getParameter("subject_id");
		request.setAttribute("academia", Academy.instance(str_academyCode));
		request.setAttribute("materia", Subject.instance(str_subjectCode, str_academyCode));
		
		DynaActionForm forma = (DynaActionForm)form;
		UnidadBean bn_unit = business.Unidades.instance(int_unitKey, str_subjectCode, str_academyCode);
		bn_unit.setAcademy(str_academyCode);
		bn_unit.setSubject(str_subjectCode);
		forma.set("int_unitKey", new Integer(bn_unit.getInt_unitId()));
		forma.set("int_unitNumber", new Integer(bn_unit.getInt_unitNumber()));
		forma.set("str_descripcion", bn_unit.getStr_descripcion());
		forma.set("int_planEstudio", new Integer(bn_unit.getInt_planEstudio()));
				
		return mapping.findForward(Util.DEFAULT); 
	}
	
	public ActionForward save(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		ActionForward action_retVal = Validate.validateUser(mapping,request, getResources(request));
		if(action_retVal!=null)return action_retVal;
		
		DynaActionForm forma = (DynaActionForm)form;
		String str_forward;
		
		String str_academyCode = request.getParameter("academy_id");
    	String str_subjectCode = request.getParameter("subject_id");
    	request.setAttribute("academia", Academy.instance(str_academyCode));    	
    	request.setAttribute("materia", Subject.instance(str_subjectCode, str_academyCode));
		
    	HttpSession session = request.getSession();
		PlanEstudioBean bn_planEstudio = (PlanEstudioBean)session.getAttribute("inst_contenido");
		int int_planEstudio = bn_planEstudio.getInt_planEstudio();
		
		if( ((Integer)forma.get("int_unitKey")).intValue() == 0 ){
			if( business.Unidades.add(forma.getMap(), int_planEstudio) > 0 ){
				request.setAttribute("req_msg", getResources(request).getMessage("DraftManage.SuccessAdd"));
				str_forward = Util.SUCCESS;
			}else{
				request.setAttribute("req_msg", getResources(request).getMessage("DraftManage.FailAdd"));
				str_forward = Util.FAIL+Util.ADD;
			}
		}else{
			if( business.Unidades.edit(forma.getMap()) > 0 ){
				request.setAttribute("req_msg", getResources(request).getMessage("DraftManage.SuccessEdit"));
				str_forward = Util.SUCCESS;
			}else{
				request.setAttribute("req_msg", getResources(request).getMessage("DraftManage.FailAdd"));
				str_forward = Util.FAIL+Util.ADD;
			}
		}
		
		if(str_forward.equals(Util.SUCCESS)){
			request.setAttribute("lst_unidad", business.Unidades.list(bn_planEstudio.getInt_planEstudio(), str_academyCode, str_subjectCode));			
		}
		return mapping.findForward(str_forward); 
	
}
	public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		ActionForward action_retVal = Validate.validateUser(mapping,request, getResources(request));
		if(action_retVal!=null)return action_retVal;
		int int_unitKey =Integer.parseInt(request.getParameter("unit_id"));
		
		String str_academyCode = request.getParameter("academy_id");
		String str_subjectCode = request.getParameter("subject_id");		
		request.setAttribute("academia", Academy.instance(str_academyCode));
		request.setAttribute("materia", Subject.instance(str_subjectCode, str_academyCode));
		
		HttpSession session = request.getSession();
		PlanEstudioBean bn_planEstudio = (PlanEstudioBean)session.getAttribute("inst_contenido");
		int int_planEstudio = bn_planEstudio.getInt_planEstudio();
		
		if( business.Unidades.delete(int_unitKey, str_academyCode, str_subjectCode) > 0 ){
			request.setAttribute("req_msg", getResources(request).getMessage("DraftManage.SuccessDelete"));
		}else{
			request.setAttribute("req_msg", getResources(request).getMessage("Units.errors.FailDelete"));
		}		
		request.setAttribute("lst_unidad", business.Unidades.list(int_planEstudio, str_academyCode, str_subjectCode));		
		return mapping.findForward(Util.DEFAULT);
	}
	
}